package view.mediator
{
	import com.google.maps.Map;
	
	import flash.events.Event;
	
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	
	import controllers.commands.ParseMobileInfoCommand;
	import controllers.commands.StartPositionCommand;
	import controllers.managers.SingletonManager;
	
	import event.LocationEvent;
	
	import model.context.TrackerContext;
	import model.context.base.IContext;
	import model.filter.MapFilter;
	import model.protocal.GpsInvalid_lbs_protocal;
	import model.protocal.base.IProtocol;
	import model.registry.EnvConst;
	import model.validator.DeviceUtil;
	
	import view.MapCanvas;
	import view.mediator.base.IMediator;
	import view.panel.LocationDataWindow;

	/**
	 * 定位数据代理器
	 **/
	public class LocationDataMediator implements IMediator
	{
		public static var NAME : String = "LocationDataMediator";
		public var playbackCMD:StartPositionCommand;
		
		private var parentContainer:Maper;
		private var container:MapCanvas;
		private var locationWindow:LocationDataWindow;
		private var map:Map;
		private var totalPosInfo:Object = {};
		private var positionArray : Array = null;
		private var cIndex : int = -1;
		
		
		public function LocationDataMediator(view:UIComponent)
		{
			if(view!=null)
			{
				
				//取出单例的playBackCMD
				playbackCMD = StartPositionCommand(SingletonManager.getServ(StartPositionCommand.NAME)); 
				setView(view);
			}
		}
		
		public function initEvents():void
		{}
		
		public function setContext(context:IContext):void
		{}
		
		public function setView(v:UIComponent):void
		{
			parentContainer = Maper(FlexGlobals.topLevelApplication);
			container = MapCanvas(v);
			if ( locationWindow == null )
			{
				locationWindow = new LocationDataWindow();
			}
		}				
		
		/**
		 * 设置显示对象
		 **/
		public function setData(data:Object):void
		{
			positionArray  = null;
			totalPosInfo = data;
			if(Global.currentMapType == 1)
			{
				positionArray = data.map;
				
			}else 
			{
				positionArray = data.sat;
			}
			
			_gpsInvalideList = [];
			var locInfo:Object;/* = currentData[maxIndex];*/
			for each(locInfo in positionArray)
			{
				if ( locInfo.personal_status == 0 ) {
					//记录无效对象
					if(parseInt(locInfo.station_info, 16)==0 && (DeviceUtil.getDeviceType()==7 || DeviceUtil.getDeviceType()==18))
					{
						continue;
					}else
					{
						_gpsInvalideList.push(locInfo);
					}
				} 
			}
			
			if(_gpsInvalideList.length>0)
			{
				gpsInvalideLbsHandle()
			}else
			{
				doShow();
			}
			
//			locationWindow.setData(positionArray);
//			locationWindow.addEventListener("myClose", onClose);
//			locationWindow.addEventListener(LocationEvent.SELECTED_ROW, onSelectedRowHandler);
//			locationWindow.addEventListener(LocationEvent.START, onStartPlaybackHandler);
//			
//			onShow();
		}
		
		private function doShow():void
		{
			locationWindow.setData(positionArray);
			locationWindow.addEventListener("myClose", onClose);
			locationWindow.addEventListener(LocationEvent.SELECTED_ROW, onSelectedRowHandler);
			locationWindow.addEventListener(LocationEvent.START, onStartPlaybackHandler);
			
			onShow();
		}
		
		private var _gpsInvalideList:Array;
		private function gpsInvalideLbsHandle():void
		{
			Global.openLoading();
			
			var locInfo:Object;
			for each(locInfo in _gpsInvalideList)
			{
				var stationInfo:String = locInfo.station_info;
				var prex:int = 4;
				var endx:int = 4;
				var len:int = stationInfo.length;
				var lac:String = Number("0x" + stationInfo.substr(0, prex)).toString(10);
				var cid:String = Number("0x" + stationInfo.substr(endx, len)).toString(10);
				var protocal:IProtocol = new GpsInvalid_lbs_protocal(lac, cid, locInfo, gpsInvalidLbsCallback);
				var parser:ParseMobileInfoCommand = new ParseMobileInfoCommand(protocal);
				parser.execute();
			}
		}
		
		private function gpsInvalidLbsCallback(data:Object):void
		{
			var i:int, locInfo:Object;
			var len:int = _gpsInvalideList.length;
			for(i=0; i<len; ++i)
			{
				locInfo = _gpsInvalideList[i];
				if(data==locInfo)
				{
					_gpsInvalideList.splice(i, 1);
				}
			}
			
			if(_gpsInvalideList.length==0)
			{
				Global.hideLoading();
				doShow();
			}
		}
		
		private function onShow():void
		{
			PopUpManager.addPopUp(locationWindow, parentContainer, true);
			PopUpManager.centerPopUp(locationWindow);
			locationWindow.y = 20;
		}
		
		public function onClose(evt:Event):void
		{
			if ( locationWindow != null )
			{
				PopUpManager.removePopUp(locationWindow);
				locationWindow = null;
			}
		}
		
		/**
		 * 开始回放
		 **/
		private function onStartPlaybackHandler(event:LocationEvent):void
		{
			if ( locationWindow != null )
			{				
				//执行回放
				if(positionArray != null && cIndex >= 0)
				{
					setLocData(positionArray);
				}
				
				PopUpManager.removePopUp(locationWindow);
				locationWindow = null;
			}
		}
		
		public function setLocData(positionArray:Array):void
		{
			var context : TrackerContext = new TrackerContext();
			context.workType = EnvConst.TYPE_LOCATION_DATA;
			context.mapCanvas = this.container;	
			positionArray = MapFilter.updateArray(positionArray);
			totalPosInfo.map = positionArray; 
			totalPosInfo.sat = positionArray;
			context.totalPos = totalPosInfo;

			playbackCMD.setContext(context);
			playbackCMD.execute();
		}
		
		/**
		 * 选择行事件
		 **/
		private function onSelectedRowHandler(evt:LocationEvent):void
		{
			var selectedRow:Object = evt.selectedRow;
			if(selectedRow != null)
			{
//				GlobeEventDispatcher.dispatchEvent(evt);
				onSelectedStartRowHandler(evt);
			}
		}			
		
		/**
		 * 当选择一行开始回放数据记录的时候
		 **/
		private function onSelectedStartRowHandler(evt:LocationEvent):void
		{
			if(positionArray != null && evt.selectedIndex >= 0)
			{				
				cIndex = evt.selectedIndex;
				positionArray = positionArray.slice(evt.selectedIndex);
			}
		}
	}
}